<?php
require dirname(__FILE__) . DIRECTORY_SEPARATOR . 'autoload.php';

use payClass\Datebase;
use payClass\QRcode;
use payClass\pay\Config;

/**
 * 扫码支付
 */
class Scan
{
	public static function scanDo()
	{
		$db     = new Datebase();

		$config = new Config();

		$param = $db->param();

		// 订单号
		$param['orderNumber'] = 'C' . time() . 'U' . $param['uid'];

		$param['paytype'] = Config::$folder.'-'.Config::$merchant;
		// 数据检测
		// $verifyRes = $db->verifyScan($param);

		// if ($verifyRes != 'Y') $db->ajaxReturn($verifyRes);

		$data = [
			'merchant_code' => Config::$merchant,
			'appno_no'      => Config::$appid,
			'order_no'      => $param['orderNumber'],
			'order_amount'  => sprintf("%.2f", $param['price']),
			'order_time'    => date('Ymd'),
			'product_name'  => date('Ymd'),
			'product_code'  => date('Ymd'),
			'user_no'       => $param['uid'],
			'notify_url'    => Config::$notifyUrl,
			'pay_type'      => $param['scanType'],
			'return_url'    => Config::$merUrl,
		];
		// JSON编码
		$dataArray['transdata'] = urlencode(json_encode($data));
		// 生成签名
		$sign                   = Config::getSignMsg($data, Config::$key);

		$dataArray['sign']      = urlencode($sign);
		// 签名类型
		$dataArray['signtype']  = 'MD5';
		// 发送请求
		$curlRes                = Config::setHttpRequest(Config::$subUrl, json_encode($dataArray));
		// JSON解码
		$resArray               = json_decode($curlRes, true);

		if (!$resArray['payment']) $db->ajaxReturn($resArray['message']);

		$db->ajaxReturn($resArray['payUrl']);
	}
}

Scan::scanDo();
